<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_jicai/model')->方法名();
*/
namespace jy_jicai;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function up_task($cangid=0){
		if(!$_SESSION['uniacid']){
			return false;
		}
		$this->up_cang();
		if($cangid){
			$cang=pdo_get('jy_jicai_cang',array('id'=>$cangid,'uniacid'=>$_SESSION['uniacid']));
			if($cang['status']){
				return false;
			}
			$cangs[0]=$cang;
		}else{
		$cangs=pdo_getall('jy_jicai_cang',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
		}
		foreach($cangs as $v){
			$this->up_yao($v);
			if(!$v['status'] && $v['startime']<=TIMESTAMP){
				$this->start_yao($v);
			}
		}
	}
	public function up_cang(){
		if(!$this->setting['open_cang']){
			return false;
		}
		$item=pdo_get('jy_jicai_cang',array('status'=>0,'uniacid'=>$_SESSION['uniacid']));
		if(!$item['id']){
			$paixu=pdo_count('jy_jicai_cang',array('uniacid'=>$_SESSION['uniacid']));
			$data=array(
						'uniacid'=>$_SESSION['uniacid'],
						'allget_rate'=>$this->setting['allget_rate']?:30,
						'jian_num'=>$this->setting['jian_num']?:3,
						'jian_jinum'=>$this->setting['jian_jinum']?:3000,
						'jian_jinrate'=>$this->setting['jian_jinrate']?:33,
						'jian_jinmin'=>$this->setting['jian_jinmin']?:1,
						'jian_jinmax'=>$this->setting['jian_jinmax']?:100,
						'jian_upjinmax'=>$this->setting['jian_upjinmax']?:100,
						'jian_cannum'=>$this->setting['jian_cannum']?:3,
						'jian_getrate'=>$this->setting['jian_getrate']?:10,
						'jian_tonum'=>$this->setting['jian_tonum']?:2,
						'jian_tobi'=>$this->setting['jian_tobi']?:50,
						'jian_tofee'=>$this->setting['jian_tofee']?:50,
						'paixu'=>$paixu+1,
						'createtime'=>TIMESTAMP,
						'status'=>0,
			);
			$this->insert('cang',$data);
			$data['id']=pdo_insertid();
			$this->add_yao($data);
		}
		return true;
	}
	public function add_yao($item){
		if($item['status']){
			return false;
		}
		$nownum=pdo_count('jy_jicai_yao',array('status <='=>1,'cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$yaonum=pdo_count('jy_jicai_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		if($yaonum<$item['jian_num'] && $nownum<=2){
			$is7=pdo_get('jy_jicai_yao',array('status'=>7,'cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
			if($is7['id']){
				return false;
			}
			$jian_days=0;
			$jian_jinrate=$item['jian_jinrate'];
			$jian_upjinmax=$item['jian_upjinmax'];
			$jian_days=$this->setting['jian_days']?:3;
			if($item['jian_num']-$yaonum>3){
				$jian_num=$yaonum+3;
			}else{
				$jian_num=$item['jian_num'];
			}
			$lastyao=pdo_get('jy_jicai_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']),'','id desc');
			$startime=$lastyao['endtime']?:TIMESTAMP;
			$jian_jinum=$lastyao['alljifee']?:$item['jian_jinum'];
			$jian_jinmax=$lastyao['jian_jinmax']?:$item['jian_jinmax'];
			for($i=$yaonum;$i<$jian_num;$i++){
				$startime +=$jian_daystime;
				if($i){
					$jian_jinum=intval((100+$jian_jinrate)*$jian_jinum/100);
					$jian_jinmax=$jian_jinmax+$jian_upjinmax;
				}
				$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'cangid'=>$item['id'],
					'alljifee'=>$jian_jinum,
					'jian_jinmax'=>$jian_jinmax,
					'paixu'=>$i+1,
					'createtime'=>TIMESTAMP,
					'startime'=>$startime,
					'endtime'=>$startime+$jian_days*24*3600,
					'status'=>$i?0:1,
				);
				$this->insert('yao',$data);
				$jian_daystime=$jian_days*24*3600;
			}
		}
		return true;
	}
	public function up_yao($yao){
		if($yao['status']!=1){
			return false;
		}
		if($yao['endtime']<=TIMESTAMP){
			$this->up_outtime($yao);
		}
		return true;
	}
	public function start_yao($yao){
		//刷新状态；
		$yao['status']=pdo_getcolumn('jy_jicai_yao',array('id'=>$yao['id']),'status');
		if($yao['status']){
			return $yao['status'];
		}
		if(!$yao['status'] && $yao['startime']<=TIMESTAMP){
			pdo_update('jy_jicai_yao',array('status'=>1),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
			$yao['status']=1;
			$yuelogs=pdo_getall('jy_jicai_yuelog',array('status'=>0,'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
			foreach($yuelogs as $item){
				$this->up_canlog($item);
			}
			pdo_update('jy_jicai_yuelog',array('status'=>1),array('status'=>0,'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		return $yao['status'];
	}
	public function up_outtime($yao=array(),$jian_getrate=0){
		if($yao['endtime']>TIMESTAMP){
			return false;
		}
		//未开始的停建
		pdo_update('jy_jicai_yao',array('status'=>7,'successtime'=>TIMESTAMP),array('status'=>0,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_jicai_yao',array('status'=>3,'successtime'=>TIMESTAMP),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
		pdo_update('jy_jicai_cang',array('status'=>2,'successtime'=>TIMESTAMP),array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function up_sifang($cang=array(),$jian_getrate=0){
		if($cang['status']!=2 && $cang['status']!=3){
			exi('释放失败,该仓不支持手动平仓！','error');
		}
		$manyaos=pdo_count('jy_jicai_yao',array('cangid'=>$cang['id'],'status'=>array(2,5),'uniacid'=>$_SESSION['uniacid']));
		//未建满的1窖是否释放
		if($cang['status']==2 && !$this->setting['pingc_isto'] && $manyaos<1){
			//exi('释放失败,未建满的1窖不能释放！','error');	
		}
		if($cang['status']==3 && !$this->setting['pingc_isto2'] && $manyaos<1){
			//exi('释放失败,未建满的1窖不能释放！','error');	
		}
		$status=array(2,3,4);
		$params=array('cangid'=>$cang['id'],'status'=>$status,'uniacid'=>$_SESSION['uniacid']);
		$yaos=pdo_getall('jy_jicai_yao',$params);
		$num=pdo_count('jy_jicai_cang',array('id <='=>$cang['id'],'uniacid'=>$_SESSION['uniacid']));
		$cangtitle='第【'.$num.'】仓';
		foreach($yaos as $yao){
					$pingc_totype=$this->setting['pingc_totype'];
					$pingc_tobi=$this->setting['pingc_tobi'];
					$pingc_tofee=$this->setting['pingc_tofee'];
					$title=$yao['status']==3?'未建满释放':'收益封顶平仓释放';
					$yaotitle=$yao['paixu']?'第【'.$yao['paixu'].'】窖':'';
					$title=$cangtitle.$yaotitle.$title;
					$canlogs=pdo_getall('jy_jicai_canlog',array('yaoid'=>$yao['id'],'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
					$feelog=array(
						'cangid'=>$yao['cangid'],
						'yaoid'=>$yao['id'],
					);
					foreach($canlogs as $cv){
						$getfee=$cv['jifen'];
						$feelog['mid']=$cv['mid'];
						$feelog['canlogid']=$cv['id'];
						if($yao['status']!=3 || $this->setting['pingc_isto']){
							if($pingc_tobi){
								$fee=$pingc_tobi*$getfee/100;
								$feelog['fee']=$fee;
								if($pingc_totype==1){
									model('core/member')->member_credit($cv['mid'],'credit1',$fee,true,'jy_jicai',$title);
									$this->add_feelog($feelog,14);
								}else{
									model('core/member')->member_credit($cv['mid'],'credit4',$fee,true,'jy_jicai',$title);
									$this->add_feelog($feelog,2);
								}
							}
							if($pingc_tofee){
								$fee=$pingc_tofee*$getfee/100;
								$feelog['fee']=$fee;
								if($pingc_totype==2){
									model('core/member')->member_credit($cv['mid'],'credit1',$fee,true,'jy_jicai',$title);
									$this->add_feelog($feelog,14);
								}else{
									model('core/member')->member_credit($cv['mid'],'credit2',$fee,true,'jy_jicai',$title);
									$this->add_feelog($feelog,3);
								}
							}
						}else{
							model('core/member')->member_credit($cv['mid'],'credit1',$getfee,true,'jy_jicai',$cangtitle.$yaotitle.'积分返回');
							$feelog['fee']=$getfee;
							$this->add_feelog($feelog,12);
						}
					}
					pdo_update('jy_jicai_canlog',array('status'=>3),array('yaoid'=>$yao['id'],'status'=>array(0,1),'uniacid'=>$_SESSION['uniacid']));
					pdo_update('jy_jicai_yao',array('status'=>6),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
		}
		pdo_update('jy_jicai_cang',array('status'=>4),array('id'=>$cang['id'],'uniacid'=>$_SESSION['uniacid']));
		return true;
	}
	public function up_success($yaoid){
		if(!$yaoid){
			return false;
		}
		$yao=pdo_get('jy_jicai_yao',array('id'=>$yaoid,'uniacid'=>$_SESSION['uniacid']));
		if(!$yao['id']){
			return false;
		}
		$isfee=pdo_getsum('jy_jicai_canlog',array('yaoid'=>$yaoid,'uniacid'=>$_SESSION['uniacid']),'jifen');
		$cang=pdo_get('jy_jicai_cang',array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
		$jian_getrate=$cang['jian_getrate'];
		if($isfee>=$yao['alljifee']){
			pdo_update('jy_jicai_yao',array('status'=>2,'successtime'=>TIMESTAMP),array('id'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
			pdo_update('jy_jicai_canlog',array('status'=>1),array('yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']));
			$num=pdo_count('jy_jicai_yao',array('status'=>2,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
			if($num>=$cang['jian_tonum']){
				$isyao=pdo_get('jy_jicai_yao',array('status'=>2,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']),'','id asc');
				if($isyao['id']){
					pdo_update('jy_jicai_yao',array('status'=>5),array('id'=>$isyao['id'],'uniacid'=>$_SESSION['uniacid']));
					$canlogs=pdo_getall('jy_jicai_canlog',array('yaoid'=>$isyao['id'],'uniacid'=>$_SESSION['uniacid']));
					pdo_update('jy_jicai_canlog',array('status'=>2),array('yaoid'=>$isyao['id'],'uniacid'=>$_SESSION['uniacid']));
					foreach($canlogs as $cv){
						$this->up_tofee($cv,$jian_getrate);
					}
					
				}
			}
			$isnum=pdo_count('jy_jicai_yao',array('status >'=>1,'cangid'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
			if($isnum>=$cang['jian_num']){
				pdo_update('jy_jicai_cang',array('status'=>1),array('id'=>$yao['cangid'],'uniacid'=>$_SESSION['uniacid']));
			}
		}
		return true;
	}
	public function up_tofee($canlog,$jian_getrate=0){
		if(!$canlog || !$jian_getrate){
			return false;
		}
		$getfee=$canlog['jifen']*$jian_getrate/100;
		$fee=$canlog['jifen']+$getfee;
		$num=pdo_count('jy_jicai_cang',array('id <='=>$canlog['cangid'],'uniacid'=>$_SESSION['uniacid']));
		$cangtitle='第【'.$num.'】仓';
		$yao=pdo_get('jy_jicai_yao',array('id'=>$canlog['yaoid'],'uniacid'=>$_SESSION['uniacid']));
		$yaotitle='第【'.$yao['paixu'].'】期';
		pdo_update('jy_jicai_member',array('baofee +='=>$fee),array('mid'=>$canlog['mid'],'uniacid'=>$_SESSION['uniacid']));
		$canlog['getfee']=$getfee;
		$canlog['fee']=$fee;
		$this->add_feelog($canlog,0);
		$this->add_jilog($canlog,0);
		$this->model('team')->add_commission($canlog,$cangtitle,$yaotitle);
		$this->model('commission')->create_area_rate($canlog,$cangtitle,$yaotitle);
		return true;
	}
	public function up_canginfo($item){
		$day=date('Y-m-d',$item['createtime']);
		$startime=strtotime($day);
		$endtime=$startime+24*3600;;
		$num=pdo_count('jy_jicai_cang',array('id <='=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$item['isyaonum']=pdo_count('jy_jicai_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>array(2,5)));
		$now=pdo_get('jy_jicai_yao',array('cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid'],'status'=>1));
		$item['nowyaonum']=pdo_count('jy_jicai_yao',array('id <='=>$now['id'],'cangid'=>$item['id'],'uniacid'=>$_SESSION['uniacid']));
		$item['title']='第【'.$num.'】仓';
		$item['desc']='已建好【'.$item['isyaonum'].'】'.$this->y_title.'，第'.$item['nowyaonum'].''.$this->y_title.'建设中，最低可投入（'.$item['jian_jinmin'].'）积分，每次投入积分建设收益'.$item['jian_getrate'].'%';
		return $item;
	}
	public function get_cangtitle($cangid){
		if(!$cangid){
			return '';
		}
		$num=pdo_count('jy_jicai_cang',array('id <='=>$cangid,'uniacid'=>$_SESSION['uniacid']));
		$title='第【'.$num.'】仓';
		return $title;
	}
	public function team_level_up_types(){
		return str_array($this->teamsetting['level_up_types']);
	}
	public function current_level_up_types(){
		return str_array($this->agentsetting['level_up_types']);
	}
	public function add_feelog($item,$type=0){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'canlogid'=>$item['canlogid']?:$item['id'],
					'fee'=>$item['fee'],
					'type'=>$type,
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('feelog',$data);
		return true;
	}
	public function add_jilog($item,$type=0){
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'canlogid'=>$item['canlogid']?:$item['id'],
					'jifen'=>$item['fee'],
					'type'=>$type,
					'createtime'=>TIMESTAMP,
					'status'=>0,
		);
		$this->insert('jilog',$data);
		return true;
	}
	public function up_canlog($item,$isxunibi=0){
		$btype=1;
		if($isxunibi){
			$btype=$this->setting['jian_feetype']?:6;
		}
		$data=array(
					'uniacid'=>$_SESSION['uniacid'],
					'mid'=>$item['mid']?:$_SESSION['uid'],
					'cangid'=>$item['cangid'],
					'yaoid'=>$item['yaoid'],
					'jifen'=>$item['jifen'],
					'dayid'=>date('Y-m-d'),
					'createtime'=>TIMESTAMP,
					'status'=>0,
					'isxunibi'=>$isxunibi
		);
		$this->insert('canlog',$data);
		$canlogid=pdo_insertid();
		model('core/member')->member_credit($_SESSION['uid'],'credit'.$btype,$item['jifen'],false,'jy_jicai','参与建仓','canlogid'.$canlogid);
		$this->up_success($item['yaoid']);
		$data['fee']=$data['jifen'];
		$data['canlogid']=$canlogid;
		$this->add_feelog($data,4);
		return true;
	}
	public function check_buy($yao,$fee=0,$isxunibi=0){
		$iscan=pdo_count('jy_jicai_canlog',array('mid'=>$_SESSION['uid'],'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid'],'dayid'=>date('Y-m-d')),'id');
		$team=pdo_get('jy_jicai_cang',array('uniacid'=>$_SESSION['uniacid'],'id'=>$yao['cangid']));
		if($iscan>=$team['jian_cannum'] && $this->setting['jian_mid']!=$_SESSION['uid']){
			exi('您今天已参与'.$iscan.'次，请明天再来吧！','error');
		}
		$credit_types=credit_types(true);
		if($isxunibi && ($this->setting['jian_allfeerate'] || $this->setting['jian_maxfeenum'] || $this->setting['jian_onemaxfeenum'])){
			$isfee=pdo_getsum('jy_jicai_canlog',array('isxunibi'=>1,'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
            $oneisfee=pdo_getsum('jy_jicai_canlog',array('mid'=>$_SESSION['uid'],'isxunibi'=>1,'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
            if($this->setting['jian_onemaxfeenum']){
                $lastfee3=$this->setting['jian_onemaxfeenum']-$oneisfee;
                if($fee>$lastfee3){
                    exi($credit_types[$this->setting['jian_feetype']].'最大建仓总额限制，当前剩余'.$lastfee3.'可投入！','error');
                }
            }
            $lastfee2=$this->setting['jian_maxfeenum']-$isfee;
			$lastfee1=$yao['alljifee']*$this->setting['jian_allfeerate']/100-$isfee;
			$lastfee=$lastfee1<$lastfee2?$lastfee1:$lastfee2;
			$lastfee=intval($lastfee)+1;
			if($fee>$lastfee){
				exi($credit_types[$this->setting['jian_feetype']].'最大建仓总额限制，当前剩余'.$lastfee.'可投入！','error');
			}
		}elseif(!$isxunibi && $this->setting['jian_allfeerate1']){
			$isfee=pdo_getsum('jy_jicai_canlog',array('isxunibi'=>0,'yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
			$lastfee=$yao['alljifee']*$this->setting['jian_allfeerate1']/100-$isfee;
			$lastfee=intval($lastfee)+1;
			if($fee>$lastfee){
				exi('积分最大建仓总额限制，当前剩余'.$lastfee.'可投入积分！','error');
			}
		}else{
			$isfee=pdo_getsum('jy_jicai_canlog',array('yaoid'=>$yao['id'],'uniacid'=>$_SESSION['uniacid']),'jifen');
			$lastfee=$yao['alljifee']-$isfee;
			$lastfee=intval($lastfee)+1;
			if($fee>$lastfee){
				exi('当前剩余'.$lastfee.'可投入积分！','error');
			}
		}
		return true;
	}
}
?>